/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Data;
using System.Data.SqlClient;
using Votations.NSurvey.Web.Security;
using Votations.NSurvey.Data;
using Votations.NSurvey.DataAccess;

namespace Votations.NSurvey.UserProvider
{
	/// <summary>
	/// Implements the default provider to support 
	/// DNN's users authentication
	/// </summary>
	public class DNNUserProvider : IUserProvider
	{
		/// <summary>
		/// Creates a new principal based on the provided username
		/// </summary>
		public INSurveyPrincipal CreatePrincipal(string userName)
		{
			bool isAdmin = false,
				 hasGlobalSurveyAccess = false;
			
			if (userName == null || userName.Length == 0)
			{
				return new NSurveyFormPrincipal(
					new NSurveyDNNIdentity("anonymous", -1, isAdmin, hasGlobalSurveyAccess, false), null);
			}

			//What you must do here is take the DNN userid that
			//s stored in the user name 
			int userId = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().UserID;
			string username = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().Username;

			// Get the nsurvey rights from the database
			int[] userRights = new Users().GetUserSecurityRights(userId);

			// Get user settings and if available assign them to 
			// the local variables
			UserSettingData userSettings = new Users().GetUserSettings(userId);
			if (userSettings.UserSettings.Rows.Count > 0)
			{
				isAdmin = userSettings.UserSettings[0].IsAdmin;
				hasGlobalSurveyAccess = userSettings.UserSettings[0].GlobalSurveyAccess;
			}

			return new NSurveyDNNPrincipal(
				new NSurveyDNNIdentity(username, userId, isAdmin, hasGlobalSurveyAccess, true), 
				userRights);
		}

		/// <summary>
		/// Retrieves all users from the database
		/// </summary>
		public UserData GetAllUsersList()
		{
			// If not DNN host account get only users for the current portal
			DotNetNuke.Entities.Portals.PortalSettings portal = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
			DotNetNuke.Entities.Users.UserController userControl = new DotNetNuke.Entities.Users.UserController();
			System.Collections.ArrayList users = userControl.GetUsers(portal.PortalId,false,false);

			UserData userData = new UserData();
			foreach (DotNetNuke.Entities.Users.UserInfo user in users)
			{
				UserData.UsersRow newUser = userData.Users.NewUsersRow();
				newUser.UserId = user.UserID;
				newUser.UserName = user.Username;
				userData.Users.Rows.Add(newUser);
			}

			System.Web.HttpContext.Current.Trace.Warn(string.Format("Total Users: {0}",users.Count.ToString()));
			return userData;
		}
	}
}
